$(document).ready(function(){
	
	/*----------------------------------------
	Backend Society Verify
	----------------------------------------*/
	$('#society_verify_dialog').dialog({
		autoOpen: false,
		//show: "slide",
		//hide: "slide",
		resizable: false,
		draggable: false,
		modal: true,
		width: 900,
		height: 630
	});
	
	/* 社团详细信息 */
	$(".detail_dialog_link").live("click", function(){
		$("#society_verify_dialog").dialog("open");
		$("#society_verify_dialog").spin();
		var society_id = $(this).attr("id");
		$.ajax({
			type: 'POST',
			url: "/index.php/backend/association/verify/ajax_get_so_info",
			dataType: 'json',
			cache: false,
			data: {society_id: society_id},
			success: function(data) {
				$("#society_verify_dialog").spin(false);
				if (data['society'].confirm_FLAG == "0;0;0") {
					var society_name = data['society']['s_name'];
					$("#society_verify_dialog").dialog({
						buttons: {
							"审核通过": function() {
								ajax_society_verify_pass(society_id, society_name);
							},
							"审核失败": function() { 
								ajax_society_verify_failed(society_id, society_name);
							},
							"取消": function() {
								$("#society_verify_dialog").dialog("close");
							}
						}
					});
				} else {
					$("#society_verify_dialog").dialog({
						buttons: {
							"取消": function() {
								$("#society_verify_dialog").dialog("close");
							}
						}
					});
				}
				open_society_verify_dialog(data);
				//$("#society_verify_dialog").dialog("open");
			}
		});
	});
	
	function open_society_verify_dialog(data) {
		
		/*往社团基本信息栏及社团负责人信息栏中加内容*/
		document.getElementById("ui-dialog-title-society_verify_dialog").innerHTML = data['society']['s_name'] + "(" + getSocietyStatusByFlag(data['society']['confirm_FLAG']) + ") - 初审资料";
		for (x in data['society']) {
			if (document.getElementById(x)) {
				if (data['society'][x] != null) {
					if (x != "contact_col" && x != "contact_pro" && x != "s_category") {
						document.getElementById(x).innerHTML = data['society'][x];
					} else if (x == "s_category") {
						document.getElementById("s_category").innerHTML = getSocietyCategoryNameById(data['society']['s_category']);
					}
				} else {
					document.getElementById(x).innerHTML = "尚未填写";
				}
			}
		}
		if (data['society']['contact_col'] != null && data['society']['contact_pro'] != null) {
			document.getElementById("col_pro").innerHTML = getColProNameById(data['society']['contact_col']) + " - " + getColProNameById(data['society']['contact_pro']);
			$("#col_pro").attr("title", getColProNameById(data['society']['contact_col']) + " - " + getColProNameById(data['society']['contact_pro']));
			//<!-- 截取字符串 -->
			$("#col_pro").each(function(){
				if ($(this).text().length > 22) {
					$(this).text($(this).text().substring(0, 22) + "...");
				}
			});
		} else {
			document.getElementById("col_pro").innerHTML = "尚未填写";
		}
		
		/*往社团指导老师信息栏中加内容*/
		for (x in data['teacher']) {
			if (document.getElementById(x)) {
				if (data['teacher'][x] != null) {
					if (x != "t_birth") {
						document.getElementById(x).innerHTML = data['teacher'][x];
					} else {
						document.getElementById(x).innerHTML = convert_birthday(data['teacher'][x]);
					}
				} else {
					document.getElementById(x).innerHTML = "尚未填写";
				}
			}
		}
	}
	
	function ajax_society_verify_pass(society_id, society_name) {
		$(".confirm_dialog").remove();
		$("#content").append(
			"<div class='confirm_dialog' title='确认你的审核结果?'>" +
				"<form>" +
				"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:9px 5px 5px 0;'></span>该社团状态将被设置为<spans>初审通过</spans>，你确定吗？若选是，请填写备注</p>" +
				"<textarea id='remark' style='width:320px; height:120px' class='text ui-widget-content ui-corner-all'></textarea>" +
				"</form>" +
			"</div>"
		);
		
		$(".confirm_dialog").dialog({
			resizable: false,
			draggable: false,
			modal: true,
			width: 373,
			height: 330,
			buttons: {
				"是": function() {
					var remark = document.getElementById("remark").value;
					$.ajax({
						url: '/index.php/backend/association/verify/ajax_verify_pass',
						type: 'POST',
						cache: false,
						data: {society_id: society_id, society_name: society_name, remark: remark},
						success: function() {
							window.location.reload();
						}
					});
				},
				"否": function() {
					$(this).dialog( "close" );
				}
			}
		});
	}
	
	function ajax_society_verify_failed(society_id, society_name) {
		$(".confirm_dialog").remove();
		$("#content").append(
			"<div class='confirm_dialog' title='确认你的审核结果?'>" +
				"<form>" +
				"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:9px 5px 5px 0;'></span>该社团状态将被设置为<spans>初审失败</spans>，你确定吗？若选是，请填写备注</p>" +
				"<textarea id='remark' style='width:320px; height:120px' class='text ui-widget-content ui-corner-all'></textarea>" +
				"</form>" +
			"</div>"
		);
		$(".confirm_dialog").dialog({
			resizable: false,
			draggable: false,
			modal: true,
			width: 373,
			height: 330,
			buttons: {
				"是": function() {
					var remark = document.getElementById("remark").value;
					$.ajax({
						url: '/index.php/backend/association/verify/ajax_verify_failed',
						type: 'POST',
						cache: false,
						data: {society_id: society_id, society_name: society_name, remark: remark},
						success: function() {
							window.location.reload();
						}
					});
				},
				"否": function() {
					$(this).dialog( "close" );
				}
			}
		});
	}
	
	$('#society_verify_log_dialog').dialog({
		autoOpen: false,
		//show: "slide",
		//hide: "slide",
		resizable: false,
		draggable: false,
		modal: true,
		width: 840,
		height: 500,
		buttons: {
			"确定": function() {
				$(this).dialog("close");
			}
		}
	});
	
	/* 社团审核日志 */
	$(".log_dialog_link").live("click", function(){
		$("#society_verify_log_dialog").dialog("open");
		$("#society_verify_log_dialog").spin();
		var society_id = $(this).attr("id");
		$.ajax({
			type: 'POST',
			url: "/index.php/backend/association/verify/ajax_get_so_log",
			dataType: 'json',
			cache: false,
			data: {society_id: society_id},
			success: function(data) {
				$("#society_verify_log_dialog").spin(false);
				open_society_verify_log_dialog(data);
			}
		});
	});
	
	function open_society_verify_log_dialog(data) {
		$("#society_verify_log_dialog").empty();
		/*往社团审核日志对话框中加内容*/
		document.getElementById("ui-dialog-title-society_verify_log_dialog").innerHTML = data['s_name'] + " - 审核日志";
		if (data['signin_log'] == "") {
			$("#society_verify_log_dialog").append("<spans>该社团暂时没有审核日志</spans>");
			return;
		}
		
		$("#society_verify_log_dialog").append(
			"<table id='log_table' class='full'>" +
			"<thead>" +
			"<tr>" +
				"<th>执行人</th>" +
				"<th style='text-align:center;' >执行人角色</th>" +
				"<th style='text-align:center;' >社团状态</th>" +
				"<th style='text-align:center;' >执行时间</th>" +
				"<th style='text-align:center;' >备注</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody>" +
			"</tbody>" +
            "</table>"
		);
		for (var i = data['signin_log'].length - 1; i >= 0; i--) {
			$("#log_table tbody").append(
				"<tr>" +
                "<td style='text-align:center; width:80px;' >" + data['signin_log'][i]['execute_user'] + "</td>" +
                "<td style='text-align:center; width:80px;' >" + data['signin_log'][i]['execute_role'] + "</td>" +
                "<td style='text-align:center; width:70px;' >" + data['signin_log'][i]['status'] + "</td>" +
                "<td style='text-align:center; width:70px;' >" + data['signin_log'][i]['time'] + "</td>" +
                "<td><spans>" + data['signin_log'][i]['remark'] + "</spans></td>" +
                "</tr>"
			);
		}
	}
	
	$('#oneclick_verify_confirm_dialog').dialog({
		autoOpen: false,
		//show: "slide",
		//hide: "slide",
		resizable: false,
		draggable: false,
		modal: true,
		width: 420,
		//height: 500,
		buttons: {
			"确定": function() {
				$("#progressbar").progressbar({
					value: 100
				});
				$.ajax({
					type: 'POST',
					url: "/index.php/backend/association/verify/ajax_oneclick_verify",
					dataType: 'json',
					cache: false,
					success: function(data) {
						if (data == "failed") {
							$("#content").append(
								"<div class='confirm_dialog' title='警告'>" +
									"<p>现在还没有能够使用一键审核的社团，请稍后再试</p>" +
								"</div>"
							);
							$(".confirm_dialog").dialog({
								resizable: false,
								draggable: false,
								modal: true,
								//width: 373,
								//height: 330,
								buttons: {
									"知道了": function() {
										window.location.reload();
									}
								}
							});
						} else if (data == "success") {
							$(".ui-progressbar-value").append("&nbsp;&nbsp;一键审核已完成，页面即将重载...");
							setTimeout("window.location.reload();", 1000);
						}
					}
				});
			},
			"取消": function() {
				$(this).dialog("close");
			}
		}
	});
	
	/*----------------------------------------
	Backend Delete News and Announcement
	----------------------------------------*/
	
	$('.delete').live('click', function() {
		var id = $(this).attr('id');
		if ($(this).hasClass("news")) {
			var category = "news";
			var category_cn = "新闻";
		} else if ($(this).hasClass("announcement")) {
			var category = "announcement";
			var category_cn = "公告";
		}
		ajax_delete_news_announcement(id, category, category_cn)
	});
	
	function ajax_delete_news_announcement(id, category, category_cn) {
		$(".confirm_dialog").remove();
		
		$("#content").append(
			"<div class='confirm_dialog' title='确认删除?'>" +
				"<form>" +
				"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:9px 5px 5px 0;'></span>确定要删除该" + category_cn + "吗?<br />" + category_cn + "删除后将无法恢复</p>" +
				"</form>" +
			"</div>"
		);
		$(".confirm_dialog").dialog({
			resizable: false,
			draggable: false,
			modal: true,
			//width: 373,
			//height: 330,
			buttons: {
				"是": function() {
					$.get('/index.php/backend/association/' + category + '/delete/' + id,
						function(){
							window.location.reload();
						}
					);
				},
				"否": function() {
					$(this).dialog( "close" );
				}
			}
		});
	}
	
	/*----------------------------------------
	Backend Society Verify
	----------------------------------------*/
	$('#employee_info_dialog').dialog({
		autoOpen: false,
		//show: "slide",
		//hide: "slide",
		resizable: false,
		draggable: false,
		modal: true,
		width: 500,
		height: 390
	});
	
	/* 社团详细信息 */
	$(".detail_employee_info_link").live("click", function(){
		$("#employee_info_dialog").dialog("open");
		$("#employee_info_dialog").spin();
		var employee_id = $(this).attr("id");
		$.ajax({
			type: 'POST',
			url: "/index.php/backend/association/employee/ajax_get_employee_info",
			dataType: 'json',
			cache: false,
			data: {employee_id: employee_id},
			success: function(data) {
				$("#employee_info_dialog").spin(false);
				$("#employee_info_dialog").dialog({
					buttons: {
						"确定": function() {
							$("#employee_info_dialog").dialog("close");
						}
					}
				});
				
				open_detail_employee_info_dialog(data);
			}
		});
	});
	
	function open_detail_employee_info_dialog(data) {
		
		/*往社团基本信息栏及社团负责人信息栏中加内容*/
		document.getElementById("ui-dialog-title-employee_info_dialog").innerHTML = data['a_name'] + " - 详细信息";
		for (x in data) {
			if (document.getElementById(x)) {
				if (data[x] != null) {
					if (x != "a_gender" && x != "a_birth" && x != "a_category" && x != "a_col" && x != "a_pro") {
						document.getElementById(x).innerHTML = data[x];
					} else if (x == "a_gender") {
						document.getElementById("a_gender").innerHTML = getGenderNameById(data['a_gender']);
					} else if (x == "a_birth") {
						document.getElementById("a_birth").innerHTML = convert_birthday(data['a_birth']);
					} else if (x == "a_category") {
						document.getElementById("a_category").innerHTML = getAssociationCategoryNameById(data['a_category']);
					}
				} else {
					document.getElementById(x).innerHTML = "尚未填写";
				}
			}
		}
		if (data['a_col'] != null && data['a_pro'] != null) {
			document.getElementById("col_pro").innerHTML = getColProNameById(data['a_col']) + " - " + getColProNameById(data['a_pro']);
			$("#col_pro").attr("title", getColProNameById(data['a_col']) + " - " + getColProNameById(data['a_pro']));
			//<!-- 截取字符串 -->
			$("#col_pro").each(function(){
				if ($(this).text().length > 22) {
					$(this).text($(this).text().substring(0, 22) + "...");
				}
			});
		} else {
			document.getElementById("col_pro").innerHTML = "尚未填写";
		}
	}
});